home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / printing / docprn / docprint.sx next >
Encoding:
Text File  |  1996-05-21  |  2.4 KB  |  91 lines  |  [TEXT/ttxt]

  1. --<<<-
  2. -- Filename:
  3. --     DOCPRINT.SX
  4.  
  5. -- Other Files Required: 
  6. --     NONE
  7.  
  8. -- Purpose:
  9. --     Defines new subclasses of Document and Window and provides implementations
  10. --       of printDocument and printWindow to render them to a printer.
  11. in module docModule
  12.  
  13. class MyDocumentClass (Document)
  14. end
  15.  
  16. method printDocument self {class MyDocumentClass} -> (
  17.     local p, scaleFactor, myTransform, oldTransform, \
  18.           oldPosition, oldPresentedBy
  19.     
  20.     -- Create a Printer
  21.     p := new PrinterSpace
  22.     myTransform := mutableCopy identityMatrix
  23.     
  24.     oldPresentedBy := self.presentedBy
  25.     prepend p self
  26.               
  27.     -- Start the document and see if we should continue
  28.     if (printerDialog p) do (
  29.         local firstPage, lastPage, i, n
  30.  
  31.         -- Record the firstPage and lastPage
  32.         firstPage := p.firstPage
  33.         p.firstPage := 1
  34.         lastPage := p.lastPage
  35.         p.lastPage := @all
  36.  
  37.         -- Clip the page range to the extent of the document
  38.         if ((lastPage = @all) or (lastPage > self.size)) do
  39.             lastPage := self.size
  40.  
  41.         -- Record the old state of the Document, so that we can reset it
  42.         oldPosition := self.cursor
  43.         oldTransform := self.transform
  44.  
  45.         for i := firstPage to lastPage do (
  46.             -- If this page is in the correct page range
  47.             -- Go to that page and find out how best to
  48.             -- scale it. Since different pages can have
  49.             -- different boundaries, we have to do it everytime.
  50.             goto self i
  51.             scaleFactor := min (p.boundary.width / \
  52.                                 self.boundary.width)  \
  53.                                 (p.boundary.height /  \
  54.                                 self.boundary.height)
  55.  
  56.             -- Set the transform based on the scaling factor
  57.             reset myTransform
  58.             scale myTransform scaleFactor scaleFactor
  59.             self.transform := myTransform
  60.  
  61.             -- Take a snapshot of the page
  62.             printFrame p
  63.  
  64.             -- Move on to the next page
  65.             flushPage p
  66.         )
  67.  
  68.         -- Flush the document
  69.         flushDocument p
  70.          
  71.         -- Reset the state of the document
  72.         goto self oldPosition
  73.         self.transform := oldTransform
  74.     )
  75.  
  76.     prepend oldPresentedBy self
  77. )
  78.  
  79. class MyWindowClass (Window)
  80. end
  81.  
  82. method printWindow self {class MyWindowClass} -> (
  83.      local doc := self[1]
  84.      self.compositor.enabled := false
  85.      
  86.      printDocument doc 
  87.      
  88.      self.compositor.enabled := true
  89. )
  90. -->>>
  91.